//
//  MNDUserAssetViewController.m
//  minidai
//
//  Created by Steven.Yung on 2017/5/17.
//  Copyright © 2017年 迷你金服. All rights reserved.
//

#import "MNDUserAssetViewController.h"
#import "MNDUserAssetModel.h"
#import "MCPieChartView.h"
#import "UIButton+JKImagePosition.h"

@interface MNDUserAssetViewController () <MCPieChartViewDataSource, MCPieChartViewDelegate>

//总资产
@property (weak, nonatomic) IBOutlet UILabel *totalCapicalLabel;
//可用余额
@property (weak, nonatomic) IBOutlet UILabel *usableBalanceLabel;
//出借中总本金
@property (weak, nonatomic) IBOutlet UILabel *borrowMoneyLabel;
//自助投出借本金
@property (weak, nonatomic) IBOutlet UILabel *plusBorrowMoneyLabel;
//散标出借本金
@property (weak, nonatomic) IBOutlet UILabel *commonBorrowMoneyLabel;

//冻结金额
@property (weak, nonatomic) IBOutlet UILabel *frozenMoneyLabel;
//已赚收益
@property (weak, nonatomic) IBOutlet UILabel *earnMoneyLabel;
//已赚自助投出借收益
@property (weak, nonatomic) IBOutlet UILabel *earnPlusBorrowMoneyLabel;
//已赚散标出借收益
@property (weak, nonatomic) IBOutlet UILabel *earnCommonBorrowMoneyLabel;
//已赚平台奖励收益
@property (weak, nonatomic) IBOutlet UILabel *awardBorrowMoneyLabel;
//预期收益
@property (weak, nonatomic) IBOutlet UILabel *expectMoneyLabel;
//预期自助投出借收益
@property (weak, nonatomic) IBOutlet UILabel *earnPlusExpectLabel;
//预期散标出借收益
@property (weak, nonatomic) IBOutlet UILabel *earnCommonExpectLabel;
//预期奖励收益
@property (weak, nonatomic) IBOutlet UILabel *awardExpectMoneyLabel;
//累计出借
@property (weak, nonatomic) IBOutlet UILabel *totalLendMoneyLabel;
//累计借款
@property (weak, nonatomic) IBOutlet UILabel *totalBorrowMoneyLabel;
//累计充值
@property (weak, nonatomic) IBOutlet UILabel *totalRechargeLabel;
//累计提现
@property (weak, nonatomic) IBOutlet UILabel *totalWithdrawLabel;
//饼状图
@property (strong, nonatomic) MCPieChartView *pieChartView;
@property (weak, nonatomic) IBOutlet UIView *pieChartContainerView;

@property (strong, nonatomic) NSMutableArray *pieChartDataSource;
@property (strong, nonatomic) NSArray *pieChartColors;

@property (strong, nonatomic) MNDUserAssetModel *assetModel;

//@property (weak, nonatomic) IBOutlet UIImageView *topGapImage;
//@property (weak, nonatomic) IBOutlet UIImageView *centerGapImage;
//@property (weak, nonatomic) IBOutlet UIImageView *bottomLeftImage;
//@property (weak, nonatomic) IBOutlet UIImageView *bottomRightImage;
@property (weak, nonatomic) IBOutlet UIView *borrowMoneyView;
@property (weak, nonatomic) IBOutlet UIView *earnMoneyView;
@property (weak, nonatomic) IBOutlet UIView *expectMoneyView;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *borrowMoneyViewHeight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *earnMoneyViewHeight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *expectMoneyViewHeight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *borrowMoneyDetailBottomConstraint;

@property (weak, nonatomic) IBOutlet UIImageView *earnMoneyArrowImageView;
@property (weak, nonatomic) IBOutlet UIImageView *expectMoneyArrowImageView;
@property (weak, nonatomic) IBOutlet UIImageView *borrowMoneyArrowImageView;

@end

@implementation MNDUserAssetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.pieChartDataSource = [NSMutableArray array];
    self.pieChartColors = @[[UIColor colorWithRed:79/255.0 green:140/255.0 blue:239/255.0 alpha:1.0],
                            [UIColor colorWithRed:90/255.0 green:200/255.0 blue:251/255.0 alpha:1.0],
                            [UIColor colorWithRed:249/255.0 green:192/255.0 blue:3/255.0 alpha:1.0]
                           ];
    self.borrowMoneyViewHeight.constant = 0;
    self.borrowMoneyDetailBottomConstraint.constant = 0;
    self.earnMoneyViewHeight.constant = 0;
    self.expectMoneyViewHeight.constant = 0;
//    UIImage *image = [[UIImage imageNamed:@"investment_record_gap"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 1, 0, 1)];

//    self.topGapImage.image      = image;
//    self.centerGapImage.image   = image;
//    self.bottomLeftImage.image  = image;
//    self.bottomRightImage.image = image;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self loadAssetDetail];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self.pieChartContainerView sendSubviewToBack:self.pieChartView];
}

- (void)loadAssetDetail {
    
    WeakSelf(self);
    [self.view beginLoading];

    [APIManager sendRequestWithPath:API_USER_ASSET_LIST parameters:[MNDSign signedParam] method:kXMHTTPMethodGET onSuccess:^(ResponseModel *responseModel) {
        [weakself.view endLoading];
        
        if (responseModel.status == ResponseSuccess) {
            
            weakself.assetModel = [MNDUserAssetModel mj_objectWithKeyValues:responseModel.data];
            [weakself configContents];
        } else {
            NSDictionary *dict = responseModel.msg;
            if (dict && dict.count > 0) {
                [NetworkProgressHUD showHudMsg:dict[@"error"]];
                
            } else {
                [NetworkProgressHUD showHudMsg:@"获取数据失败"];
            }
        }
    } onFailure:^(NSError *error) {
        [NetworkProgressHUD showHudNetworkError:error];
        [weakself.view endLoading];
    }];
}

- (void)configContents {
    //资产总额
    self.totalCapicalLabel.text = self.assetModel.userAmountAssets;
    //可用余额
    self.usableBalanceLabel.text = self.assetModel.userAmountAvailable;
    //出借总本金
    self.borrowMoneyLabel.text = self.assetModel.userCurrentTenderAmount;
    //自助投出借本金
    self.plusBorrowMoneyLabel.text = self.assetModel.userCurrentTenderPlanAmount;
    //散标出借本金
    self.commonBorrowMoneyLabel.text = self.assetModel.userCurrentTenderTenderAmount;
    //冻结金额
    self.frozenMoneyLabel.text = self.assetModel.userAmountFrozen;
    //已赚收益
    self.earnMoneyLabel.text = self.assetModel.userIncomeTotal;
    //已赚自助投出借收益
    self.earnPlusBorrowMoneyLabel.text = self.assetModel.userIncomePlanInterestTotal;
    //已赚散标出借收益
    self.earnCommonBorrowMoneyLabel.text = self.assetModel.userIncomeTenderInterestTotal;
    //已赚平台奖励收益
    self.awardBorrowMoneyLabel.text = self.assetModel.userIncomeRewardTotal;
    //预期收益总额
    self.expectMoneyLabel.text = self.assetModel.userWaitIncomeAmount;
    //预期自助投出借收益
    self.earnPlusExpectLabel.text = self.assetModel.userWaitIncomePlanInterestTotal;
    //预期散标投出借收益
    self.earnCommonExpectLabel.text = self.assetModel.userWaitIncomeTenderInterestTotal;
    //预期奖励收益
    self.awardExpectMoneyLabel.text = self.assetModel.userWaitIncomeRewardTotal;
    //累计出借
    self.totalLendMoneyLabel.text = self.assetModel.userTenderAmount;
    //累计借款
    self.totalBorrowMoneyLabel.text = self.assetModel.userAmountBorrow;
    //累计充值
    self.totalRechargeLabel.text = self.assetModel.userAmountRecharge;
    //累计提现
    self.totalWithdrawLabel.text = self.assetModel.userAmountWithdraw;
    //可用余额
    double balance = [NSNumber numberWithString:self.assetModel.userAmountAvailable].doubleValue;
    //当前投资
    double tender = [NSNumber numberWithString:self.assetModel.userCurrentTenderAmount].doubleValue;
//    double waitInterest = [NSNumber numberWithString:self.assetModel.userWaitInterestAmount].doubleValue;
    //冻结金额
    double frozen   = [NSNumber numberWithString:self.assetModel.userAmountFrozen].doubleValue;
    double total = balance + tender + frozen;
    
    if (self.pieChartDataSource.count != 0) {
        [self.pieChartDataSource removeAllObjects];
    }
   
    if (balance == 0 && tender == 0 && frozen == 0) {
        [self.pieChartDataSource addObject:@(100/3.0)];
        [self.pieChartDataSource addObject:@(100/3.0)];
        [self.pieChartDataSource addObject:@(100/3.0)];
    } else {
        [self.pieChartDataSource addObject:@(balance/total * 100.0)];
        [self.pieChartDataSource addObject:@(tender/total * 100.0)];
        [self.pieChartDataSource addObject:@(frozen/total * 100.0)];
    }
    
    [self.pieChartView reloadDataWithAnimate:YES];
}

#pragma mark - MCPieChartDatasource

- (NSInteger)numberOfPieInPieChartView:(MCPieChartView *)pieChartView {
    
    return self.pieChartDataSource.count;
}

- (id)pieChartView:(MCPieChartView *)pieChartView valueOfPieAtIndex:(NSInteger)index {
    
    return self.pieChartDataSource[index];
}

- (id)sumValueInPieChartView:(MCPieChartView *)pieChartView {
    
    return @100.00;
}

- (UIColor *)pieChartView:(MCPieChartView *)pieChartView colorOfPieAtIndex:(NSInteger)index {
//    NSDictionary *dict = self.contents[index];
    
    return self.pieChartColors[index];
}

- (MCPieChartView *)pieChartView {
    if (!_pieChartView) {
        _pieChartView = [[MCPieChartView alloc] initWithFrame:self.pieChartContainerView.bounds];
        _pieChartView.dataSource = self;
        _pieChartView.delegate = self;
        _pieChartView.ringWidth = 30.0;
        [self.pieChartContainerView addSubview:self.pieChartView];
    }
    
    return _pieChartView;
}

#pragma mark - Action
//出借本金
- (IBAction)showOrHideBorrowMoneyDetailAction:(UIButton *)sender {
    sender.selected = !sender.isSelected;
    if (sender.selected) {
        [UIView animateWithDuration:0.25 animations:^{
            self.borrowMoneyArrowImageView.transform = CGAffineTransformMakeRotation(M_PI);
        }];
        self.borrowMoneyViewHeight.constant = 77;
        self.borrowMoneyDetailBottomConstraint.constant = 21;
        for (UIView *view in self.borrowMoneyView.subviews) {
            view.hidden = NO;
        }
        
    }else {
        [UIView animateWithDuration:0.25 animations:^{
            self.borrowMoneyArrowImageView.transform = CGAffineTransformMakeRotation(-2*M_PI);
        }];
        self.borrowMoneyViewHeight.constant = 0;
        self.borrowMoneyDetailBottomConstraint.constant = 0;
        for (UIView *view in self.borrowMoneyView.subviews) {
            view.hidden = YES;
        }
    }
}

//已赚收益
- (IBAction)showOrHiddenEarnMoneyDetailAction:(UIButton *)sender {
    
    sender.selected = !sender.isSelected;
    if (sender.selected) {
        [UIView animateWithDuration:0.25 animations:^{
           self.earnMoneyArrowImageView.transform = CGAffineTransformMakeRotation(M_PI);
        }];
        self.earnMoneyViewHeight.constant = 107;
        for (UIView *view in self.earnMoneyView.subviews) {
            view.hidden = NO;
        }
        
    }else {
        [UIView animateWithDuration:0.25 animations:^{
            self.earnMoneyArrowImageView.transform = CGAffineTransformMakeRotation(-2*M_PI);
        }];
        self.earnMoneyViewHeight.constant = 0;
        for (UIView *view in self.earnMoneyView.subviews) {
            view.hidden = YES;
        }
    }
}

- (IBAction)showOrHiddenExpectMoneyDetailAction:(UIButton *)sender {
    
    sender.selected = !sender.isSelected;
    if (sender.selected) {
        [UIView animateWithDuration:0.25 animations:^{
            self.expectMoneyArrowImageView.transform = CGAffineTransformMakeRotation(M_PI);
        }];
        
        self.expectMoneyViewHeight.constant = 107;
        for (UIView *view in self.expectMoneyView.subviews) {
            view.hidden = NO;
        }
   
    } else {
        [UIView animateWithDuration:0.25 animations:^{
          self.expectMoneyArrowImageView.transform = CGAffineTransformMakeRotation(-2*M_PI);
        }];
        self.expectMoneyViewHeight.constant = 0;
        for (UIView *view in self.expectMoneyView.subviews) {
            view.hidden = YES;
        }
        
    }
    
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
